package com.ylr.blog.framework.service.impl;

import com.ylr.base.common.po.user.UserPo;
import com.ylr.blog.framework.service.BlogFlowService;
import com.ylr.base.common.utils.UserUtils;
import com.ylr.blog.framework.entity.BlogFlowEntity;
import com.ylr.blog.framework.mapper.BlogFlowMapper;
import com.ylr.base.framework.service.SnowflakeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-19 20:34:55
 * className: BlogFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogFlowService")
@AllArgsConstructor
public class BlogFlowServiceImpl extends ServiceImpl<BlogFlowMapper, BlogFlowEntity> implements BlogFlowService {

    private static final Logger log = LoggerFactory.getLogger(BlogFlowServiceImpl.class);

    private final SnowflakeService snowflakeService;

    @Override
    public void saveBlogFlow(Long blogId, Integer status, String remark) {

        // 获取正在保存博客流水的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        BlogFlowEntity blogFlow = this.getNewBlogFlow(blogId, status, remark, userPo, date);

        // 保存博客流水
        baseMapper.insert(blogFlow);
        log.info("博客流水保存完成");
    }

    /**
     * 生成博客流水实体对象
     * @param blogId 博客主键
     * @param status 状态
     * @param remark 备注
     * @param userPo 操作用户对象
     * @param date   日期对象
     * @return 博客流水实体对象
     */
    private BlogFlowEntity getNewBlogFlow(Long blogId, Integer status, String remark, UserPo userPo, Date date) {
        BlogFlowEntity blogFlow = new BlogFlowEntity();
        blogFlow.setId(snowflakeService.getId())
                .setBlogId(blogId)
                .setStatus(status)
                .setRemark(remark)
                .setCreateTime(date)
                .setCreateUser(userPo.getUsername())
                .setCreateUserId(userPo.getId());
        return blogFlow;
    }
}